package com.zbest.zpay.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zbest.common.lang.enums.BizCode;
import com.zbest.common.lang.exception.ZpayException;
import com.zbest.common.lang.util.JSONUtil;
import com.zbest.zpay.service.apiserver.model.ApiServerKeyEnum;
import com.zbest.zpay.shared.process.ProcessService;

public class BaseController {

    private static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    @Resource
    protected ProcessService    processService;

    protected String newErrorResult(BizCode bizCode, String message, Object result) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put(ApiServerKeyEnum.KEY_CODE.getCode(), bizCode.code());
        if (StringUtils.isBlank(message)) {
            map.put(ApiServerKeyEnum.KEY_MSG.getCode(), bizCode.desc());
        } else {
            map.put(ApiServerKeyEnum.KEY_MSG.getCode(), message);
        }
        map.put(ApiServerKeyEnum.KEY_DATA.getCode(), result);
        return JSONUtil.toJson(map);
    }

    protected String newErrorResult(BizCode bizCode, String message) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put(ApiServerKeyEnum.KEY_CODE.getCode(), bizCode.code());
        if (StringUtils.isBlank(message)) {
            map.put(ApiServerKeyEnum.KEY_MSG.getCode(), bizCode.desc());
        } else {
            map.put(ApiServerKeyEnum.KEY_MSG.getCode(), message);
        }

        return JSONUtil.toJson(map);
    }

    protected String newErrorResult(BizCode bizCode) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put(ApiServerKeyEnum.KEY_CODE.getCode(), bizCode.code());
        map.put(ApiServerKeyEnum.KEY_MSG.getCode(), bizCode.desc());

        return JSONUtil.toJson(map);
    }

    protected String newSuccessResult(Object result) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(ApiServerKeyEnum.KEY_CODE.getCode(), BizCode.SUCCESS.code());
        map.put(ApiServerKeyEnum.KEY_MSG.getCode(), BizCode.SUCCESS.desc());
        map.put(ApiServerKeyEnum.KEY_DATA.getCode(), result);
        return JSONUtil.toJson(map);
    }

    protected String newSuccessResult() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(ApiServerKeyEnum.KEY_CODE.getCode(), BizCode.SUCCESS.code());
        map.put(ApiServerKeyEnum.KEY_MSG.getCode(), BizCode.SUCCESS.desc());
        return JSONUtil.toJson(map);
    }

    @ExceptionHandler
    @ResponseBody
    public String dealException(Throwable throwable) {
        logger.error("请求发生异常", throwable);
        if (throwable instanceof ZpayException) {
            ZpayException e = (ZpayException) throwable;
            return newErrorResult(e.getCode());
        }
        return newErrorResult(BizCode.UNKNOWN);
    }

    protected String getRemoteAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        ip = ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
        return ip;
    }

    protected String getAgent(HttpServletRequest request) {
        return request.getHeader("user-agent");
    }
}
